//
//  CHomeViewController.m
//  ZQReadProject
//
//  Created by 2.5 on 2021/9/7.
//

#import "CHomeViewController.h"
#import "CRootViewController.h"
#import "AppDelegate.h"
#import "CHttpManager.h"
#import "CCWebViewViewController.h"
#import <MJRefresh/MJRefresh.h>
@interface CHomeViewController()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)UITableView *tableView;
@end
@implementation CHomeViewController
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.dataArray = [[NSMutableArray alloc] init];
    self.page = 1;
    [self customeCCUI];
    [self requestData];
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.tableView.frame = CGRectMake(0, 0, self.view.ccwidth, self.view.ccheight);
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    if([delegate isKindOfClass:[AppDelegate class]]){
        CRootViewController *cvc = delegate.window.rootViewController;
        if([cvc isKindOfClass:[CRootViewController class]]){
            [cvc reloadData];
        }
    }
}
-(void)customeCCUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 33, 33)];
    homeButton.layer.masksToBounds = YES;
    homeButton.layer.cornerRadius = 22;
    [homeButton setImage:[UIImage imageNamed:@"head_default44"] forState:UIControlStateNormal];
    [homeButton addTarget:self action:@selector(selectedMineVCAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:homeButton];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.ccwidth, self.view.ccheight-50) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    MJWeakSelf;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf requestData];
    }];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        weakSelf.page++;
        [weakSelf requestData];
    }];
}

-(void)requestData
{
    kWeakSelf;
    NSString *str= [NSString stringWithFormat:@"http://122.10.161.2/api/v4/news?page=%ld&pageSize=20",(long)self.page];
    [CHttpManager requestGET:str andParam:@{} andSuccess:^(NSDictionary * _Nonnull reslutDic) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        NSArray *dataArray = reslutDic[@"data"];
        if(weakSelf.page ==1){
            [weakSelf.dataArray removeAllObjects];
        }
        if([dataArray isKindOfClass:[NSArray class]]){
            [weakSelf.dataArray addObjectsFromArray:dataArray];
        }
        [weakSelf.tableView reloadData];
    } andFailed:^(NSError * _Nullable error, NSDictionary * _Nullable resultDic) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"news"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"news"];
    }
    if(self.dataArray.count>indexPath.row){
        NSDictionary *infoDic = self.dataArray[indexPath.row];
        cell.textLabel.text = NotNilStr(infoDic[@"title"]);
        cell.detailTextLabel.text = NotNilStr(infoDic[@"summary"]);
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.dataArray.count>indexPath.row){
        NSDictionary *infoDic = self.dataArray[indexPath.row];
        CCWebViewViewController *cvc = [[CCWebViewViewController alloc] init];
        cvc.isNormal = YES;
        cvc.title = NotNilStr(infoDic[@"title"]);
        cvc.baseUrl = NotNilStr(infoDic[@"my_url"]);
        cvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:cvc animated:YES];
    }
    
}
-(void)selectedMineVCAction
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    if([delegate isKindOfClass:[AppDelegate class]]){
        CRootViewController *cvc = delegate.window.rootViewController;
        if([cvc isKindOfClass:[CRootViewController class]]){
            [cvc showOrHidLeftAction];
        }
    }
}
@end
